﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace ModelResourceManager.QueueModel
{
    public class EntityQueueManager
    {
        public bool Add(Queue queue)
        {
            if (queue == null)
                return false;
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    container.Queues.Add(queue);
                    container.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(Queue queue)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    if (container.Users.Any())
                    {
                        container.Entry(queue).State = EntityState.Modified;
                        container.SaveChanges();
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(Guid id)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    var currentQueue = this.GetCurrentQueue(id);
                    if (currentQueue == default(Queue))
                        return false;
                    container.Queues.Remove(currentQueue);
                    container.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Queue GetCurrentQueue(Guid id)
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    var currentQueue = container.Queues
                                                .FirstOrDefault(resource =>
                                                                resource.ResourceId == id);
                    if (currentQueue == default(Queue))
                        return null;
                    return currentQueue;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Queue> GetAll()
        {
            try
            {
                using (var container = new ResourceManagerEFContainer())
                {
                    if (container.Queues == null || !container.Queues.Any())
                        return null;
                    return container.Queues.OrderBy(queue => queue.StartTime).ToList();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<Queue> GetAllForUser(Guid userId)
        {
            List<Queue> allQueues = GetAll();
            if (allQueues == null || !allQueues.Any())
                return null;
            IEnumerable<Queue> queues = allQueues.Where(queue => queue.UserId == userId);
            return queues;
        }

        public IEnumerable<Queue> GetAllForResource(Guid resourceId)
        {
            List<Queue> allQueues = GetAll();
            if (allQueues == null || !allQueues.Any())
                return null;
            IEnumerable<Queue> queues = allQueues.Where(queue => queue.ResourceId == resourceId);
            return queues;
        }

    }
}